<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0,user-scalable=no,minimal-ui">
    <title>菩提阁</title>
    <link rel="icon" href="./../images/favico.ico">
    <!--不同屏幕尺寸根字体设置-->
    <script src="./../js/base.js"></script>
    <!--element-ui的样式-->
    <link rel="stylesheet" href="../../backend/plugins/element-ui/index.css"/>
    <!--引入vant样式-->
    <link rel="stylesheet" href="../styles/vant.min.css"/>
    <!-- 引入样式  -->
    <link rel="stylesheet" href="../styles/index.css"/>
    <!--本页面内容的样式-->
    <link rel="stylesheet" href="./../styles/user-edit.css"/>
</head>
<body>
<div id="user_edit" class="app">
    <div class="divHead">
        <div class="divTitle">
            <i class="el-icon-arrow-left" @click="goBack"></i>修改个人信息
        </div>
    </div>
    <div class="divContent">
        <div class="divItem">
            <span>用户名：</span>
            <el-input placeholder=" 请填写用户名" v-model="form.name" maxlength='10' class="inputUser"/>
            </el-input>
            <span class="spanChecked" @click="form.sex = 1">
                    <i :class="{iActive:form.sex === 1}"></i>
                    先生
                   </span>
            <span class="spanChecked" @click="form.sex = 0">
                    <i :class="{iActive:form.sex === 0}"></i>
                    女士
                </span>
        </div>
        <div class="divItem">
            <span>验证码：</span>
            <el-input placeholder=" 请输入验证码" v-model="code" maxlength='20'/>
            </el-input>
            <span @click='getCode'>获取验证码</span>
        </div>
        <div class="divItem">
            <span>新密码：</span>
            <el-input placeholder=" 请填写新密码" type="password" v-model="form.password" maxlength='20'/>
            </el-input>
        </div>
        <div class="divMsg" v-if="msgFlag">修改密码需要输入验证码，请输入验证码</div>
        <div class="divSave" @click="saveUser">保存修改</div>
        <div class="divDelete" @click="goBack">取消</div>
    </div>
</div>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="../../backend/plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../backend/plugins/element-ui/index.js"></script>
<!-- 引入vant样式 -->
<script src="./../js/vant.min.js"></script>
<script src="./../js/common.js"></script>
<!-- 引入axios -->
<script src="../../backend/plugins/axios/axios.min.js"></script>
<script src="./../js/request.js"></script>
<script src="./../api/user.js"></script>
<script src="./../api/login.js"></script>
<script>
    new Vue({
        el: "#user_edit",
        data() {
            return {
                form: {
                    id: undefined,
                    name: '',//联系人
                    phone: undefined,//手机号
                    sex: 1,//0表示女 1 表示男
                    password: '',//密码
                },
                code: '',
                msgFlag: false,
                id: undefined,
                activeIndex: 0
            }
        },
        computed: {},
        created() {
            this.initData()
        },
        mounted() {
        },
        methods: {
            goBack() {
                history.go(-1)
            },
            async initData() {
                const params = parseUrl(window.location.search)
                const res = await userFindOneApi(params)
                if (res.code === 1) {
                    this.form = res.data
                } else {
                    this.$notify({type: 'warning', message: res.msg});
                }
            },
            // 获取验证码
            getCode() {
                this.code = ''
                const regex = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
                if (regex.test(this.form.phone)) {
                    this.msgFlag = false
                    const code = sendMsgApi({phone: this.form.phone});
                    sessionStorage.setItem("code", code);
                } else {
                    this.msgFlag = true
                }
            },
            async saveUser() {
                const form = this.form
                if (!form.name) {
                    this.$notify({type: 'warning', message: '请输入联系人'});
                    return
                }
                if (this.code==='' && form.password) {
                    this.$notify({type: 'warning', message: '修改密码请先输入密码'});
                    return
                }
                let res = await updateUserApi(this.form)

                if (res.code === 1) {
                    window.requestAnimationFrame(() => {
                        window.location.replace('/front/page/user.html')
                    })
                } else {
                    this.$notify({type: 'warning', message: res.msg});
                }
            },

        }
    })
</script>
</body>
</html>
